package ee.subjects.frontend.controller.command;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ee.subjects.backend.model.data.Address;
import ee.subjects.backend.model.data.Contact;
import ee.subjects.backend.model.data.Person;
import ee.subjects.backend.model.data.subjectdbtypetablesconstants.SubjectDBTypeTablesConstants;
import ee.subjects.backend.services.PersonService;
import ee.subjects.backend.services.PersonServiceImpl;

public class SavePersonCommand implements Command {

	@Override
	public int execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try{
			// Execute validate command before save.
			Command validatePersonCommand = new ValidatePersonCommand();
			int validationResultCode = validatePersonCommand.execute(request, response);
			
			if(validationResultCode==1){
				//--- Filling Person model
				String first_name = request.getParameter("personFirstName");
				String last_name = request.getParameter("personLastName");
				String identity_code = request.getParameter("personPersonalCode");
				
				String birthDateInput = request.getParameter("personBirthDate");
				SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
				java.util.Date date = sdf1.parse(birthDateInput);
				java.sql.Date birth_date = new Date(date.getTime()); 
				
				// TODO vaja tegelikke v22rtuseid
				int created_by = 1; 		 
	
				long nowInMilliseconds = Calendar.getInstance().get(Calendar.MILLISECOND);
				Timestamp created = new Timestamp(nowInMilliseconds);
				
				Person person = new Person();
				person.setFirst_name(first_name);
				person.setLast_name(last_name);
				person.setIdentity_code(identity_code);
				person.setBirth_date(birth_date);
				person.setCreated_by(created_by);
				person.setCreated(created);
				person.setUpdated_by(created_by);
				person.setUpdated(created);
				//--- End filling Person model---------------------
				
				//--- Filling Address model
				int address_type_fk = SubjectDBTypeTablesConstants.ADDRESS_TYPE_PERSON_MAIN_ADDRESS; 
				String country = request.getParameter("personCountry");
				String county = request.getParameter("personCounty");
				String town_village = request.getParameter("personCity_Village");
				String street_address = request.getParameter("personAddres");
				String zipcode = request.getParameter("personPostalCode");
				
				Address address = new Address();
				address.setAddress_type_fk(address_type_fk);
				address.setCountry(country);
				address.setCounty(county);
				address.setTown_village(town_village);
				address.setStreet_address(street_address);
				address.setZipcode(zipcode);
				//--- End filling Address model
				
				//--- Filling Contact model
				
				
				int contact_type_email_fk=SubjectDBTypeTablesConstants.CONTACT_TYPE_EMAIL; 
				int contact_type_phone_fk=SubjectDBTypeTablesConstants.CONTACT_TYPE_PHONE; 
				
				String value_text_email=request.getParameter("personEmail"); 		
				String value_text_phone=request.getParameter("personPhone");
				
				
				
				Contact contact_email = new Contact();
				contact_email.setContact_type_fk(contact_type_email_fk);
				contact_email.setValue_text(value_text_email);
				
				Contact contact_phone = new Contact();
				contact_phone.setContact_type_fk(contact_type_phone_fk);
				contact_phone.setValue_text(value_text_phone);
				
				List<Contact> listOfContacts = new ArrayList<Contact>();
				listOfContacts.add(contact_email);
				listOfContacts.add(contact_phone);
				

				//--- End filling Contact model
				
				
				PersonService PersonService = new PersonServiceImpl();
				PersonService.insertPersonForTheFirstTime(person, address, listOfContacts);
				
				int insertedPersonId = person.getPerson();
				String insertedPersonIdStr = Integer.toString(insertedPersonId);
				
				// Redirect to edit page 
				response.sendRedirect("?mode=person&event=edit_person&id="+insertedPersonIdStr);
				
				
			}
			
		}catch(Exception ex){
			System.out.println(ex.getStackTrace());
			System.out.println(ex.getMessage());
			
		}
		return 1;
	}

}
